﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ForumLibrary;

namespace ForumLibrary.Data
{
    public class ForumGUIManager : ForumManager
    {
        public ForumGUIManager()
        {

        }
        public override object searchForNewFriend(string username, string searchedUser)
        {
            List<User> potentialFriends = _db.searchForNewFriend("", searchedUser);
            string usersFound = "";
            foreach (User potentialFriend in potentialFriends)
            {
                usersFound += potentialFriend.Username + "\n";
            }
            return usersFound;
        }

        public override object getUser(string username)
        {
            if (_db.userExists(username))
            {
                ForumUser client = _users.Find(user => user.Username.ToLower().Equals(username.ToLower()));
                string details = "";
                if (client != null)
                {
                    User user = _db.getUser(username);
                    details = "User details were sent\n";
                    details += user.FirstName + "\n";
                    details += user.LastName + "\n";
                    details += user.Email + "\n";
                    details += client.LoginState.ToString() + "\n";
                }
                return details;
            }
            return "User doesn't exist";
        }
        
        public override object getUsers()
        {
            string usernames = "";
            List<User> users = _db.getUsers();
            foreach (User username in users)
            {
                usernames += username.Username + "\n";
            }
            return usernames;
        }

        public override object getFriends(string username)
        {
            if (_db.userExists(username))
            {
                List<User> friends = _db.getFriends(username);
                string partitionedFriends = "";
                if (friends.Count > 0)
                {
                    foreach (User friend in friends)
                    {
                        partitionedFriends += friend.Username + "\n";
                    }
                }
                else
                {
                    partitionedFriends = "User has no friends!\n";
                }
                return partitionedFriends;
            }
            return "User doesn't exist";
        }

        public override object getForums()
        {
            string forumsTopics = "";
            List<Forum> forums = _db.getForums();
            foreach (Forum forum in forums)
            {
                forumsTopics += forum.Topic + "\n";
            }
            return forumsTopics;
        }

        public override object getThreads(string forumName)
        {
            string threadsHeaders = "";
            List<Thread> threads = _db.getThreads(forumName);
            foreach (Thread thread in threads)
            {
                threadsHeaders += thread.Header + _delimiter;
            }
            return threadsHeaders;
        }

        public override object getPosts(string forumName, string threadName)
        {
            string postHeaders = "";
            if (_db.forumExists(forumName))
            {
                if (_db.threadExists(forumName, threadName))
                {
                    List<Post> posts = _db.getPosts(forumName, threadName);
                    foreach (Post post in posts)
                    {
                        postHeaders += post.Header + _delimiter;
                    }
                }
            }
            return postHeaders;
        }

        public override bool isPostOwnerOrModerator(string forumName, string threadName, string postName, string username)
        {
            if (_db.moderatorExists(username))
                return true;
            if (_db.postExists(forumName, threadName, postName))
            {
                return _db.getPostOwner(forumName, threadName, postName).Equals(username);
            }
            return false;
        }

        public override string getThreadDetails(string forumName, string threadName)
        {
            string threadDetails = "";
            if (_db.forumExists(forumName))
            {
                if (_db.threadExists(forumName, threadName))
                {
                    Thread thread = _db.getThread(forumName, threadName);

                    thread.NumOfViews++;
                    threadDetails += thread.Header + _delimiter;
                    threadDetails += thread.Body + _delimiter;
                    threadDetails += thread.NumOfReplies.ToString() + _delimiter;
                    threadDetails += thread.NumOfViews.ToString() + _delimiter;
                    threadDetails += thread.Creator + _delimiter;
                    threadDetails += thread.PublishDate.ToString() + _delimiter;
                    threadDetails += thread.LastDate;
                    return threadDetails;
                }
            }
            return threadDetails;
        }

        public override string getPostDetails(string forumName, string threadName, string postName)
        {
            string postDetails = "";
            if (_db.postExists(forumName, threadName, postName))
            {
                Post post = _db.getPost(forumName, threadName, postName);

                postDetails += post.Header + _delimiter;
                postDetails += post.Body + _delimiter;
                postDetails += post.Creator + _delimiter;
                postDetails += post.PublishDate.ToString() + _delimiter;
                return postDetails;
            }
            return postDetails;
        }
    }
}
